package org.huanletao.ssm.controller;

import org.huanletao.ssm.model.RespBean;
import org.huanletao.ssm.model.User;
import org.huanletao.ssm.service.UserService;
import org.huanletao.ssm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 1. 声 明
 * 2.
 * 3. 青秀区长湖路
 * </p>
 * package: org.huanletao.ssm.controller
 * description:
 * className: UserController
 * author:  踏雪无痕
 * copyright: Copyright (c) 2022/8/25 11:43
 * since: 1.0.0
 */
@Controller
@RequestMapping("/system/users/")
public class UserController extends BaseController {

    private String prefix = "system/user/";

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/list")
    public Map<String, Object> getAllUsers() {
        Map<String, Object> map = new HashMap<>();
        List<User> all = userService.findAll();
        map.put("code",0);
        map.put("rows",all);
        map.put("total",all.size());
        return map;
    }

    @GetMapping("/getInfo")
    public User getInfo() {
        return ShiroUtils.getCurrentUser();
    }

    @GetMapping()
    public String user() {
        return prefix + "user";
    }

    @GetMapping("/selectUser")
    @ResponseBody
    public RespBean<List<User>> selectMenus() {
        List<User> sysMenuList = userService.findAll();


        return RespBean.ok("ok", sysMenuList);
    }

    @PostMapping("/saveUser")
    @ResponseBody
    public RespBean<Object> saveMenus(User sysMenu) {
        User currentUser = ShiroUtils.getCurrentUser();
        sysMenu.setCreateTime(new Date());
        sysMenu.setCreator(currentUser.getUsername());
        return toAjax(userService.save(sysMenu));
    }

    @PostMapping("deleteUser")
    @ResponseBody
    public RespBean<Object> deleteMenus(Long id) {
        return toAjax(userService.delete(id));
    }

    @PostMapping("updateUser")
    @ResponseBody
    public RespBean<Object> updateMenus(User sysMenu) {
        return toAjax(userService.update(sysMenu));
    }

}
